package unamur.iodass.gui;
/**
 * GUI Bookstore
 * @author gilles
 * @version 2.0 27/04/2013
 */

import java.awt.BorderLayout;
import java.awt.CardLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;
import javax.swing.filechooser.FileFilter;

import org.apache.log4j.Logger;

import unamur.iodass.accountancy.Accountancy;

//import be.fundp.info.util.logging.MyLogger;


public class BSWindow extends JFrame {

	private static final Logger logger = Logger.getLogger(BSWindow.class);

	private JPanel panel;
	private JLabel title;
	private JLabel lblMargin;
	private JButton bMargin;
	private JLabel lblCatalog;
	private JTextField tfMargin;
	private JButton bSend ;


	private JTextField textField_Name;
	private JTextField textField_Adr;
	private JTextField textField_path;
	private JButton btnUpload;
	private JFileChooser fc;


	public BSWindow()  {
		super();
		build();//On initialise notre fenetre
	}

	private void build(){
		//Définit un titre pour notre fenêtre
		this.setTitle("I'm a Bookstore");
		//Définit sa taille : 400 pixels de large et 100 pixels de haut
		this.setSize(800, 600);
		//Nous demandons maintenant à notre objet de se positionner au centre
		this.setLocationRelativeTo(null);
		//Termine le processus lorsqu'on clique sur la croix rouge
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setVisible(true);
		setContentPane(buildContentPane());

	}

	private JPanel buildContentPane(){
		panel = new JPanel();
		panel.setBackground(Color.white); 
		panel.setLayout(null);
		//this.add(panel);

		title = new JLabel("--- BOOKSTORE ---");
		title.setHorizontalAlignment(SwingConstants.CENTER);
		//title.setFont(new Font("Tahoma", Font.BOLD, 16));
		title.setBounds(309, 28, 200, 30);
		panel.add(title);


		lblMargin = new JLabel("Profit margin (in %) :");
		//lblMargin.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblMargin.setHorizontalAlignment(SwingConstants.CENTER);
		lblMargin.setBounds(498, 164, 200, 35);
		panel.add(lblMargin);

		tfMargin = new JTextField();
		tfMargin.setText("20");
		tfMargin.setHorizontalAlignment(SwingConstants.CENTER);
		tfMargin.setBounds(685, 167, 75, 30);
		tfMargin.setEditable(false);
		panel.add(tfMargin);

		bMargin = new JButton("Change");
		bMargin.setHorizontalAlignment(SwingConstants.CENTER);
		bMargin.setBounds(622, 211, 138, 30);
		bMargin.addActionListener(new ButtonListener());
		panel.add(bMargin);


		lblCatalog = new JLabel("Catalog :");
		//lblCatalog.setFont(new Font("Tahoma", Font.BOLD, 16));
		lblCatalog.setHorizontalAlignment(SwingConstants.CENTER);
		lblCatalog.setBounds(498, 282, 200, 20);
		panel.add(lblCatalog);

		bSend = new JButton("Send Catalog");
		bSend.setHorizontalAlignment(SwingConstants.CENTER);
		bSend.setBounds(254, 466, 200, 40);
		bSend.addActionListener(new ButtonListener());
		panel.add(bSend);

		JLabel lblNewLabel = new JLabel("- Bookstore references -");
		lblNewLabel.setBounds(50, 95, 200, 30);
		panel.add(lblNewLabel);

		JLabel lblNewLabel_Name = new JLabel("Name");
		lblNewLabel_Name.setBounds(97, 166, 75, 30);
		panel.add(lblNewLabel_Name);

		textField_Name = new JTextField();
		textField_Name.setBounds(93, 198, 175, 30);
		panel.add(textField_Name);
		textField_Name.setText("Bookstore 1");
		//textField_Name.setColumns(10);

		JLabel lblNewLabel_Adr = new JLabel("Adress");
		lblNewLabel_Adr.setBounds(97, 247, 70, 15);
		panel.add(lblNewLabel_Adr);

		textField_Adr = new JTextField();
		textField_Adr.setBounds(97, 274, 305, 30);
		panel.add(textField_Adr);
		textField_Adr.setText("Rue du centre 1 5000 Namur");
		//textField_Adr.setColumns(10);

		JLabel lblNewLabel_Mngt = new JLabel("- Management -");
		lblNewLabel_Mngt.setBounds(498, 95, 175, 30);
		panel.add(lblNewLabel_Mngt);

		textField_path = new JTextField();
		textField_path.setBounds(498, 314, 262, 30);
		panel.add(textField_path);
		//textField_path.setColumns(10);

		btnUpload = new JButton("Upload");
		btnUpload.setBounds(658, 356, 100, 27);
		btnUpload.addActionListener(new ButtonListener());
		panel.add(btnUpload);



		return panel;

	}



	class ButtonListener implements ActionListener{
		//Redéfinition de la méthode actionPerformed()
		public void actionPerformed(ActionEvent arg0) {

			if (arg0.getSource() == bMargin){	
				String margin = (String) JOptionPane.showInputDialog(null, "Configure Profit Margin (in %) ; Please enter an integer",
						"Profit Margin", JOptionPane.QUESTION_MESSAGE,null, null,"20");
				if ((margin != null) && (margin.length() > 0)) {
					tfMargin.setText(margin);
				}
				else {
					JOptionPane.showMessageDialog(null,
							"Please enter an integer for % profit margin",
							"Error",JOptionPane.ERROR_MESSAGE);
				}
			}

			if (arg0.getSource() == btnUpload){
				fc = new JFileChooser();
				fc.addChoosableFileFilter(new FileFilter() {

					public String getDescription() {
						return "CSV Files (*.csv)";
					}

					public boolean accept(File f) {
						if (f.isDirectory()) {
							return true;
						} else {
							return f.getName().toLowerCase().endsWith(".csv");
						}
					}
				});
				int returnVal = fc.showOpenDialog(null);

				if (returnVal == JFileChooser.APPROVE_OPTION) {
					File file = fc.getSelectedFile();
					String path = file.getPath();
					logger.info("Path = " + path);
					System.out.println(path);
					textField_path.setText(path);

				}

			}

			if (arg0.getSource() == bSend){
				logger.info("!!! TODO : SENDCATALOG !!! ");

			}


		}
	}
}
